import 'package:get/get.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_obj/properties_model.dart';
import 'package:yt_devops/services/device/device_sevices.dart';

class UseDeviceModel {
  final List<int> productIdList;

  UseDeviceModel({
    List<int>? productIds,
    int? productId,
  })  : productIdList = productId != null ? [productId] : (productIds ?? []),
        assert(productIds != null || productId != null);

  DeviceServices deviceServices = Get.find();

  Future<Map<dynamic, PropertiesModel>> getPropertyMap() async {
    Map<dynamic, PropertiesModel> source = {};
    await Configs.httpClient.requestWithLoading(() async {
      await Future.forEach(productIdList, (value) async {
        var map = await deviceServices.getDeviceProperty({
          'productId': value,
        });
        List properties = map['properties'];
        for (var item in properties) {
          source[item['id']] = PropertiesModel.fromJson(item);
        }
      });
    }, showLoading: false);

    return source;
  }
}
